package com.android.bookgarden.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.bookgarden.R;
import com.android.bookgarden.bean.HomeAdvBean;
import com.android.bookgarden.bean.HomeDataBean;
import com.android.bookgarden.bean.HomeDataBean;
import com.android.bookgarden.recycleview.BaseViewHolder;
import com.android.bookgarden.tabfragment.HomeTabFragment;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Created by zouqh on 2018/3/30.
 */

public class HomeDataAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private static final int TYPE_FOOTER = 0;
    private static final int ITEM_IMAGE = 1;
    private static final int ITEM_IMAGES = 2;

    private List<HomeDataBean> datas;
    private Context mContext;
    private int viewFooter;
    private View footerView;
    private int viewHeader;
    private OnItemClickListener mOnItemClickListener;
    private int itemWidth;
    private String localCity;
    private View headerView;
    private List<HomeAdvBean> homeAdvBeanList;
    private HomeTabFragment.AdvClickLister advClickLister;

    public HomeDataAdapter(List<HomeDataBean> datas, Context context) {
        this.datas = datas;
        this.mContext = context;
    }

    public void replaceAll(List<HomeDataBean> elements) {
        if (datas.size() > 0) {
            datas.clear();
        }
        datas.addAll(elements);
        notifyDataSetChanged();
    }

    public void addAll(List<HomeDataBean> elements) {
        datas.addAll(elements);
        notifyDataSetChanged();
    }
    public void setLocalCity(String city){
        this.localCity = city;
    }


    public boolean isHeader(int position){
        int a =0;
        if (0!=viewHeader){
            a = 1;
        }
        return position >= 0 && position <a;
    }

    public int getHeaderCount(){
        int a =0;
        if (0!=viewHeader){
            a = 1;
        }
        return a;
    }
    public int getFoodCount(){
        int a =0;
        if (0!=viewFooter){
            a = 1;
        }
        return a;
    }
    public boolean isFooter(int position){
        int a =0;
        if (0!=viewFooter){
            a = 1;
        }
        return position < getItemCount() && position >= getItemCount() - a;
    }
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_IMAGE) {
            return new BaseViewHolder(LayoutInflater.from(mContext).inflate(R.layout.home_item, parent, false));
        } else if (viewType ==ITEM_IMAGES){
            headerView = LayoutInflater.from(mContext).inflate(viewHeader, parent, false);
            getHeaderView();
            return new BaseViewHolder(headerView);
        }else{
            footerView = LayoutInflater.from(mContext).inflate(viewFooter, parent, false);
            return new BaseViewHolder(footerView);
        }
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        Log.e("etItemCount()----","---"+getItemCount());
        if(isHeader(position)){
            return;
        }
        if(isFooter(position)){
            return;
        }
        final int rePosition = position - getHeaderCount();
//        if (!(0!=viewHeader&&viewFooter != 0 && position == getItemCount() -getHeaderCount()-getFoodCount())) {

        int itemCount = this.getItemCount();
            int type = getItemViewType(position);
//            try {
            if (type == ITEM_IMAGE) {
                Log.e("rePosition---",""+rePosition);
                HomeDataBean item = datas.get(rePosition);
                holder.setText(R.id.name, item.getBusinessname());
                ImageView img = holder.getView(R.id.image);
                if (null != item.getImgurls() && item.getImgurls().size() > 0) {
                    Glide.with(mContext)
                            .load(item.getImgurls().get(0))
                            .placeholder(R.mipmap.loading_icon)
                            .error(R.mipmap.loading_error)
                            .dontAnimate()
                            .centerCrop()
                            .into(img);
                }
                holder.setText(R.id.onLine, item.getLearnnum() + "");
                holder.setVisible(R.id.fire, item.isIshot());
                if (item.isIscollect()) {
                    holder.setImageResource(R.id.scIcon, R.mipmap.is_collection_icon);
                } else {
                    holder.setImageResource(R.id.scIcon, R.mipmap.no_collection_icon);
                }


//                    if (item.isColletion()){
//                        holder.setImageResource(R.id.scIcon,R.mipmap.tuoyan_icon1);
//                    }
//


                if (mOnItemClickListener != null) {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("AAAAAAAAAAAAAA", "" + position);

                            mOnItemClickListener.onItemClick(rePosition, holder);
//                                if (datas.size()==position-2){
//                                    mOnItemClickListener.onItemClick(position-2, holder);
//                                }else{
//                                    mOnItemClickListener.onItemClick(position-1, holder);
//                                }

                        }
                    });
                }
            } else {

            }
//            } catch (Exception e) {
//            }
//        }
//        } else {
//            if (mOnItemClickListener != null) {
//                holder.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        mOnItemClickListener.onItemClick(datas.size() + 1, holder);
//                    }
//                });
//            }
//        }

    }

    @Override
    public int getItemCount() {
        int count = (datas == null ? 0 : datas.size());
        if (viewFooter != 0) {
            count++;
        }
        if (viewHeader != 0) {
            count++;
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        int type = ITEM_IMAGE;
        if (isFooter(position)) {
            type = TYPE_FOOTER;
            return type;
        }else  if (isHeader(position)) {
            type = ITEM_IMAGES;
            return type;
        }
//        else{
//            return type;
//        }
//        int rePosition = position - getHeaderCount();
//        int itemCount = this.getItemCount();
//        if(rePosition < itemCount){
//            return this.getItemViewType(position);
//        }
        return ITEM_IMAGE;

    }

    public void addFooterView(int footerView) {
        this.viewFooter = footerView;
        notifyItemInserted(getItemCount() - 1);
    }

    public void addHeaderView(int viewHeader,List<HomeAdvBean> list) {
        this.viewHeader = viewHeader;
        this.homeAdvBeanList = list;
        notifyItemInserted(0);
    }

    public void addHeaderView(View viewHeader) {
        this.headerView = viewHeader;
        notifyItemInserted(0);
    }

    public void setFooterVisible(int visible) {
        if (null != footerView) {
            footerView.setVisibility(visible);
        }

    }

    //设置点击事件
    public void setOnItemClickLitener(OnItemClickListener mLitener) {
        mOnItemClickListener = mLitener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, BaseViewHolder viewHolder);
    }

    public void setAdvClick(HomeTabFragment.AdvClickLister lister){
        this.advClickLister = lister;
    }

    List<String> images;
    BGABanner mbgabanner;
    /**
     * 初始化 HeaderView
     *
     * @return
     */
    private void getHeaderView() {
        mbgabanner = headerView.findViewById(R.id.mbgabanner);
        if (null==homeAdvBeanList||homeAdvBeanList.size()==0){
            return;
        }
//设置图片集合
        mbgabanner.setData(homeAdvBeanList,null);
        mbgabanner.setAdapter(new BGABanner.Adapter<ImageView, HomeAdvBean> (){

            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, HomeAdvBean model, int position) {
                itemView.setScaleType(ImageView.ScaleType.FIT_XY);
                Glide.with(mContext)
                        .load(model.getUrl())
                        .placeholder(R.mipmap.pic)
                        .error(R.mipmap.ic_launcher)
                        .dontAnimate().skipMemoryCache(true)
                        .into(itemView);
            }
        });

        mbgabanner.setDelegate(new BGABanner.Delegate() {
            @Override
            public void onBannerItemClick(BGABanner banner, View itemView, Object model, int position) {
                HomeAdvBean adv = homeAdvBeanList.get(position);
//                advClickLister.onClick(adv.getId()+"");
            }
        });

    }
}